﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace MapEditor
{
    public class ObjectMap : ObjectBase
    {
        private Image[] _frames;
        public double AnimationDelayInMS = 0;
        public bool RepeatAnimation = false;
        public bool Animate = false;
        public bool IsAnimating = false;
        public int MaxRadius = 0;

        public ObjectMap(double x, double y, int frameCount, int baseObjID) : base(x,y,baseObjID)
        {
            ObjectCanvas.Children.Clear();

            _frames = new Image[frameCount];
            for (int i = 0; i < frameCount; i++)
            {
                _frames[i] = new Image();
                ObjectCanvas.Children.Add(_frames[i]);
            }
        }

        public void SetImage(int frame, string fileName)
        {
            _frames[frame].Source = Utils.LoadImage(fileName);
            _frames[frame].Visibility = Visibility.Collapsed;
            _frames[frame].ImageOpened += new EventHandler<RoutedEventArgs>(Image_ImageOpened);
        }

        void Image_ImageOpened(object sender, RoutedEventArgs e)
        {
            Image img = (Image)sender;
            BitmapImage bi = (BitmapImage)img.Source;

            int width = bi.PixelWidth;
            int height = bi.PixelHeight;
 
            if (MaxRadius != 0)
            {
                if (bi.PixelWidth > MaxRadius)
                {
                    img.Height = MaxRadius;
                    width = MaxRadius;
                }
                if (bi.PixelWidth > MaxRadius)
                {
                    img.Width = MaxRadius;
                    height = MaxRadius;
                }
            }
            img.Visibility = Visibility.Visible;

            SetWidth(width);
            SetHeight(height);
           
        }


        public void ShowFrame(int frame, Visibility visibility)
        {
            _frames[frame].Visibility = visibility;
        }

        public override ObjectBase Clone(int maxRadius)
        {
            ObjectMap mapObject = new ObjectMap(0,0,_frames.Length, BaseObjectID);
           // mapObject.InitializeImages(_frames.Length);

            for (int i = 0; i < _frames.Length; i++)
                mapObject.SetImage(i, ((BitmapImage)_frames[0].Source).UriSource.OriginalString);
            mapObject.MaxRadius = maxRadius;
            mapObject.RepeatAnimation = false;
            mapObject.Animate = Animate;            
            mapObject.ShowFrame(0, Visibility.Visible);
            mapObject.AnimationDelayInMS = AnimationDelayInMS;
            mapObject.Description = Description;
            mapObject.ObjectName = ObjectName;
            mapObject.IsWalkable = IsWalkable;

            return (ObjectBase)mapObject;  
        }

        public override void Deselect()
        {
            SelectionRect.Visibility = Visibility.Collapsed;
        }
        public override ObjectBase Select()
        {
            SelectionRect.Visibility = Visibility.Visible;
            return (ObjectBase)this;
        }

    }
}
